/*
 * Created by JFormDesigner on Fri Aug 10 15:09:15 EDT 2007
 */

package org.timeticker.gui;

import java.util.*;
import org.jdesktop.swingx.JXTable;
import org.timeticker.WorkTypeList;
import org.timeticker.Timekeeper;
import org.timeticker.WorkType;
import org.timeticker.impl.WorkTypeImpl;

import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.border.*;

/**
 * Work types dialog
 *
 * @author Ivan Latysh
 * @version 0.1
 * @since 11-Aug-2007 : 6:11:43 PM
 */
public class WorkTypesDialog extends JDialog {

  /** Work types table model */
  private WorkTypesTableModel workTypesTableModel;

  /**
   * Construct a new Work Type dialog with given parent frame
   *
   * @param owner parent frame
   */
  public WorkTypesDialog(Frame owner) {
		super(owner);
    preInit();
		initComponents();
    postInit();
  }

  /**
   * Construct a new Work Type dialog with given parent dialog
   *
   * @param owner parent dialog
   */
  public WorkTypesDialog(Dialog owner) {
		super(owner);
    preInit();
		initComponents();
    postInit();
  }

  /**
   * Return selected work type or null
   *
   * @return selected work type or <tt>null</tt>
   */
  public WorkType getSelectedWorkType() {
    // get selected index
    int selected = workTypesTable.getSelectedRow();
    // return null when nothing selected
    if (selected==-1) return null;
    // retrieve selected object
    return workTypesTableModel.getWorkTypeList().get(selected);
  }

  /**
   * Pre-Initialize dialog
   */
  protected void preInit() {
    // get WorkTypeList
    WorkTypeList wTypes = (WorkTypeList) Timekeeper.getInstance().getObjectList(Timekeeper.WORK_TYPE_LIST);
    // create a new table model
    workTypesTableModel = new WorkTypesTableModel(wTypes);
  }

  /**
   * Post-Initialize dialog
   */
  protected void postInit() {
    //
  }

  private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - Ivan Latysh
		ResourceBundle bundle = ResourceBundle.getBundle("org.timeticker.timeticker");
		dialogPane = new JPanel();
		contentPanel = new JPanel();
		toolBar = new JToolBar();
		newWTypeBtn = new JButton();
		editWTypeBtn = new JButton();
		deleteWTypeBtn = new JButton();
		workTypesScrollPane = new JScrollPane();
		workTypesTable = new JXTable(workTypesTableModel);
		buttonBar = new JPanel();
		closeButton = new JButton();
		newWorkTypeAction = new NewWorkTypeAction();
		editWorkTypeAction = new EditWorkTypeAction();
		deleteWorkTypeAction = new DeleteWorkTypeAction();
		closeAction = new CloseAction();

		//======== this ========
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setModal(true);
		setTitle(bundle.getString("WorkTypesDialog.Title"));
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());

		//======== dialogPane ========
		{
			dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));

			// JFormDesigner evaluation mark
			dialogPane.setBorder(new javax.swing.border.CompoundBorder(
				new javax.swing.border.TitledBorder(new javax.swing.border.EmptyBorder(0, 0, 0, 0),
					"JFormDesigner Evaluation", javax.swing.border.TitledBorder.CENTER,
					javax.swing.border.TitledBorder.BOTTOM, new java.awt.Font("Dialog", java.awt.Font.BOLD, 12),
					java.awt.Color.red), dialogPane.getBorder())); dialogPane.addPropertyChangeListener(new java.beans.PropertyChangeListener(){public void propertyChange(java.beans.PropertyChangeEvent e){if("border".equals(e.getPropertyName()))throw new RuntimeException();}});

			dialogPane.setLayout(new BorderLayout());

			//======== contentPanel ========
			{
				contentPanel.setLayout(new BorderLayout());

				//======== toolBar ========
				{
					toolBar.setFloatable(false);

					//---- newWTypeBtn ----
					newWTypeBtn.setText("New");
					newWTypeBtn.setPreferredSize(new Dimension(70, 25));
					newWTypeBtn.setMaximumSize(new Dimension(70, 25));
					newWTypeBtn.setMinimumSize(new Dimension(70, 25));
					newWTypeBtn.setAction(newWorkTypeAction);
					toolBar.add(newWTypeBtn);

					//---- editWTypeBtn ----
					editWTypeBtn.setText("Edit");
					editWTypeBtn.setPreferredSize(new Dimension(70, 25));
					editWTypeBtn.setMaximumSize(new Dimension(70, 25));
					editWTypeBtn.setMinimumSize(new Dimension(70, 25));
					editWTypeBtn.setAction(editWorkTypeAction);
					toolBar.add(editWTypeBtn);

					//---- deleteWTypeBtn ----
					deleteWTypeBtn.setText("Delete");
					deleteWTypeBtn.setPreferredSize(new Dimension(70, 25));
					deleteWTypeBtn.setMaximumSize(new Dimension(70, 25));
					deleteWTypeBtn.setMinimumSize(new Dimension(70, 25));
					deleteWTypeBtn.setAction(deleteWorkTypeAction);
					toolBar.add(deleteWTypeBtn);
				}
				contentPanel.add(toolBar, BorderLayout.NORTH);

				//======== workTypesScrollPane ========
				{
					workTypesScrollPane.setViewportView(workTypesTable);
				}
				contentPanel.add(workTypesScrollPane, BorderLayout.CENTER);
			}
			dialogPane.add(contentPanel, BorderLayout.CENTER);

			//======== buttonBar ========
			{
				buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
				buttonBar.setLayout(new GridBagLayout());
				((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 80};
				((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0};

				//---- closeButton ----
				closeButton.setText("Close");
				closeButton.setAction(closeAction);
				buttonBar.add(closeButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
					GridBagConstraints.CENTER, GridBagConstraints.BOTH,
					new Insets(0, 0, 0, 0), 0, 0));
			}
			dialogPane.add(buttonBar, BorderLayout.SOUTH);
		}
		contentPane.add(dialogPane, BorderLayout.CENTER);
		pack();
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - Ivan Latysh
	private JPanel dialogPane;
	private JPanel contentPanel;
	private JToolBar toolBar;
	private JButton newWTypeBtn;
	private JButton editWTypeBtn;
	private JButton deleteWTypeBtn;
	private JScrollPane workTypesScrollPane;
	private JXTable workTypesTable;
	private JPanel buttonBar;
	private JButton closeButton;
	private NewWorkTypeAction newWorkTypeAction;
	private EditWorkTypeAction editWorkTypeAction;
	private DeleteWorkTypeAction deleteWorkTypeAction;
	private CloseAction closeAction;
	// JFormDesigner - End of variables declaration  //GEN-END:variables

	private class CloseAction extends AbstractAction {
		private CloseAction() {
			// JFormDesigner - Action initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
			// Generated using JFormDesigner Evaluation license - Ivan Latysh
			ResourceBundle bundle = ResourceBundle.getBundle("org.timeticker.timeticker");

			putValue(NAME, bundle.getString("Close"));
			// JFormDesigner - End of action initialization  //GEN-END:initComponents
		}

		public void actionPerformed(ActionEvent e) {
      setVisible(false);
		}
	}

	private class DeleteWorkTypeAction extends AbstractAction {
		private DeleteWorkTypeAction() {
			// JFormDesigner - Action initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
			// Generated using JFormDesigner Evaluation license - Ivan Latysh
			ResourceBundle bundle = ResourceBundle.getBundle("org.timeticker.timeticker");

			putValue(NAME, bundle.getString("WorkTypesDialog.DeleteWorkTypeAction.Title"));
			// JFormDesigner - End of action initialization  //GEN-END:initComponents
		}

		public void actionPerformed(ActionEvent e) {
      // get selected index
      int selectedIndex = workTypesTable.getSelectedRow();
      if (selectedIndex!=-1) {
        // get selected work type
        final WorkType workType = workTypesTableModel.getObjectAtRow(selectedIndex);
        // delete work type
        workTypesTableModel.getWorkTypeList().remove(workType);
      }
		}
	}

	private class EditWorkTypeAction extends AbstractAction {
		private EditWorkTypeAction() {
			// JFormDesigner - Action initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
			// Generated using JFormDesigner Evaluation license - Ivan Latysh
			ResourceBundle bundle = ResourceBundle.getBundle("org.timeticker.timeticker");

			putValue(NAME, bundle.getString("WorkTypesDialog.editWorkTypeAction.Title"));
			// JFormDesigner - End of action initialization  //GEN-END:initComponents
		}

		public void actionPerformed(ActionEvent e) {
      // get selected index
      int selectedIndex = workTypesTable.getSelectedRow();
      if (selectedIndex!=-1) {
        // get selected work type
        final WorkType workType = workTypesTableModel.getObjectAtRow(selectedIndex);
        // create new work type dialog
        WorkTypeDialog wtype = new WorkTypeDialog(WorkTypesDialog.this, workType);
        wtype.setVisible(true);
        // check result action
        if ("ok".equals(wtype.getAction())) {
          // add new work type
          workTypesTableModel.getWorkTypeList().set(selectedIndex, workType);
        }
        // diapose dialog
        wtype.dispose();
      }
		}
	}

	private class NewWorkTypeAction extends AbstractAction {
		private NewWorkTypeAction() {
			// JFormDesigner - Action initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
			// Generated using JFormDesigner Evaluation license - Ivan Latysh
			ResourceBundle bundle = ResourceBundle.getBundle("org.timeticker.timeticker");

			putValue(NAME, bundle.getString("WorkTypesDialog.NewWorkTypeAction.Title"));
			// JFormDesigner - End of action initialization  //GEN-END:initComponents
		}

		public void actionPerformed(ActionEvent e) {
      // create new work type dialog
      final WorkType newWorkType = new WorkTypeImpl();
      WorkTypeDialog wtype = new WorkTypeDialog(WorkTypesDialog.this, newWorkType);
      wtype.setVisible(true);
      // check result action
      if ("ok".equals(wtype.getAction())) {
        // add new work type
        workTypesTableModel.getWorkTypeList().add(newWorkType);
      }
      // diapose dialog
      wtype.dispose();
		}
	}
}
